package com.toggery.POS.controller;

import com.toggery.POS.entity.Bill;
import com.toggery.POS.entity.Inventory;
import com.toggery.POS.entity.Orderdetail;
import com.toggery.POS.entity.Orders;
import com.toggery.POS.service.BillService;
import com.toggery.POS.service.InventoryService;
import com.toggery.POS.service.OrderDetailService;
import com.toggery.POS.service.OrderService;
import com.toggery.POS.tool.DateTool;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LJH on 2018/3/18
 */
@RestController
//默认返回json格式
@RequestMapping("/bill")
@Api(tags = "账单数据")
public class BillController {
    @Autowired
    BillService service;
    @Autowired
    OrderService orderService;
    @Autowired
    OrderDetailService detailService;
    @Autowired
    InventoryService inventoryService;
    @GetMapping("/list")
    public Map findAllBill(){
        Map map = new HashMap() ;
        map.put("data",service.findAll());
        return map;
    }
    @GetMapping("/findbyorderid/{id}")
    public Map findByOrderId(@PathVariable int id){
        Map map = new HashMap() ;
        map.put("bill",service.findAllWithOrderId(id));
        return map;
    }

    @GetMapping("/findbyid/{id}")
    public Map findById(@PathVariable int id){
        Map map = new HashMap() ;
        map.put("bill",service.findById(id));
        return map;
    }

    @PostMapping("/delete/{id}")
    public Map deleteById(@PathVariable int id){
        Map map = new HashMap();
        if(service.deleteByid(id)){
            map.put("result",true);
            map.put("msg","删除成功");
        }else {
            map.put("result",false);
            map.put("msg","删除失败");
        }
        return map;
    }

    @PostMapping("/update")
    @ResponseBody
    public Map updateBill(Model model, @RequestBody(required=false) Map map1){
        Bill bill;
        bill = service.findById(Integer.parseInt(map1.get("id").toString()));
//        bill.setBillname(map1.get("billname").toString());
        HashMap map = new HashMap();
        if(service.updateBill(bill)){
            map.put("result",true);
            map.put("msg","更新成功");
        }else {
            map.put("result",false);
            map.put("msg","更新失败");
        }
        return map;
    }

    @PostMapping("/save")
    public Map saveBill(Model model, @RequestBody(required=false) Map map1){
        Bill bill = new Bill();
        bill.setCreatby("Boss");
        bill.setCreatdate(DateTool.getNow());
        int orderId = Integer.parseInt(map1.get("orderId").toString());
        int discount =Integer.parseInt(map1.get("discount").toString());
        Orders orders = orderService.findById(orderId);
        double sum = orders.getSumamount();
        sum = sum*(1-0.1*discount);
        bill.setOrderid(orders.getId());
        bill.setPaytype("现金支付");
        bill.setSumamount(sum);
        if(discount==0){
            bill.setDescription("时间"+DateTool.getNow().toString()+","+orders.getDescription()+"，使用现金支付，共收"+orders.getSumamount()+",未打折");
        }else {
            bill.setDescription("时间"+DateTool.getNow().toString()+","+orders.getDescription()+"，使用现金支付，应收"+orders.getSumamount()+"，打"+(10-discount)+"折，" +
                    ",现收"+ sum);
        }
        HashMap map = new HashMap();
        if(service.save(bill)){
            orders.setOrdertype("已支付");
            orderService.updateOrder(orders);
            outInventory(orders);
            map.put("result",true);
            map.put("result","保存成功");
        }else {
            map.put("result",false);
            map.put("result","保存失败");
        }
        return map;
    }
    //售出即出库
    void outInventory(Orders orders){
        Inventory inventory = new Inventory();
        List<Orderdetail> list = detailService.findAllWithOrderId(orders.getId());
        for (Orderdetail  orderdetail:list) {
            inventory.setCreattime(DateTool.getNow());
            inventory.setType(1);
            //出库
            inventory.setClothingid(orderdetail.getClothingid());
            inventory.setClothingname(orderdetail.getClothing());
            inventory.setNumber(orderdetail.getNumber());
            inventoryService.save(inventory);
            inventory = new Inventory();
        }
    }
}
